//
//  MCMallStatisticsHeaderView.swift
//  StoreManage
//
//  Created by MC on 2018/4/25.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

class MCMallStatisticsHeaderView: UIView {

    
    var isShowEmptyView = false {
        didSet {
            self.emptyView.isHidden = !isShowEmptyView

            if isShowEmptyView {
                self.frame = CGRect.init(x: 0, y: 0, width: 0, height: MCScreenWidth * 0.5 + 90 + 384)
            } else {
                self.frame = CGRect.init(x: 0, y: 0, width: 0, height: MCScreenWidth * 0.5 + 90)
            }
        }
    }
    
    
    var dataDict = Dictionary<String,Any>() {
        didSet {
            let orderAmount = dataDict["orderAmount"] as? NSNumber ?? 0
            let orderCount = dataDict["orderCount"] as? NSNumber ?? 0
            let orderTotalFee = dataDict["orderTotalFee"] as? NSNumber ?? 0
            let orderTotalCommision = dataDict["orderTotalCommision"] as? NSNumber ?? 0

            
            let orderAmountStr = MCString_judgemantDecimalsIsZero(figure: orderAmount.intValue)
            
            moneyLabel.attributedText = orderAmountStr.MCAttributedString_font(font: 20, range: NSRange.init(location: 0, length: 1))
            
            leftView.bottomLabel.text = MCString_judgemantDecimalsIsZero(figure: orderTotalFee.intValue)
            centerView.bottomLabel.text = orderCount.stringValue
            rightView.bottomLabel.text = MCString_judgemantDecimalsIsZero(figure: orderTotalCommision.intValue)
        }
    }
    
    

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.MCGray_light
        self.frame = CGRect.init(x: 0, y: 0, width: MCScreenWidth, height: MCScreenWidth * 0.5 + 90)
        
        self.addSubview(bgImageView)
        bgImageView.snp.remakeConstraints { (make) ->Void in
            make.left.right.top.equalTo(self)
            make.height.equalTo(MCScreenWidth * 0.5)
        }
        
        bgImageView.addSubview(promptLabel)
        promptLabel.snp.remakeConstraints { (make) ->Void in
            make.left.top.equalTo(10)
        }
        
        bgImageView.addSubview(promptOneLabel)
        promptOneLabel.snp.remakeConstraints { (make) ->Void in
            make.top.equalTo(promptLabel.snp.bottom).offset(15)
            make.centerX.equalTo(bgImageView)
        }

        
        bgImageView.addSubview(moneyLabel)
        moneyLabel.snp.remakeConstraints { (make) ->Void in
            make.top.equalTo(promptOneLabel.snp.bottom).offset(3)
            make.centerX.equalTo(bgImageView)
        }
        


        bgImageView.addSubview(leftView)
        leftView.snp.remakeConstraints { (make) ->Void in
            make.bottom.equalTo(bgImageView)
            make.top.equalTo(moneyLabel.snp.bottom).offset(5)
            make.width.equalTo(MCScreenWidth / 3)
            make.left.equalTo(bgImageView)
        }

        bgImageView.addSubview(rightView)
        rightView.snp.remakeConstraints { (make) ->Void in
            make.top.bottom.equalTo(leftView)
            make.width.equalTo(MCScreenWidth / 3)
            make.right.equalTo(bgImageView)
        }

        bgImageView.addSubview(centerView)
        centerView.snp.remakeConstraints { (make) ->Void in
            make.top.bottom.equalTo(leftView)
            make.width.equalTo(MCScreenWidth / 3)
            make.centerX.equalTo(bgImageView)
        }
        

        
        self.addSubview(beginTimeButton)
        beginTimeButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(10)
            make.top.equalTo(bgImageView.snp.bottom).offset(10)
            make.right.equalTo(self.snp.centerX).offset(-15)
            make.height.equalTo(30)
        }
        
        self.addSubview(endTimeButton)
        endTimeButton.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(-10)
            make.top.equalTo(bgImageView.snp.bottom).offset(10)
            make.left.equalTo(self.snp.centerX).offset(15)
            make.height.equalTo(30)
        }
        
        self.addSubview(promptTwoLabel)
        promptTwoLabel.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(endTimeButton)
            make.size.equalTo(CGSize.init(width: 20, height: 20))
        }
        
        self.addSubview(sureButotn)
        sureButotn.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(-10)
            make.size.equalTo(CGSize.init(width: 80, height: 30))
            make.top.equalTo(beginTimeButton.snp.bottom).offset(10)
        }
        
        self.addSubview(chooseButton)
        chooseButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(10)
            make.right.equalTo(sureButotn.snp.left).offset(-10)
            make.height.equalTo(30)
            make.centerY.equalTo(sureButotn)
        }
        
        self.addSubview(emptyView)
        emptyView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(self)
            make.top.equalTo(chooseButton.snp.bottom).offset(10)
            make.height.equalTo(384)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    @objc func chooseButtonClicked() {
        
        let window = UIApplication.shared.keyWindow
        window?.addSubview(popUpChooseView)
        popUpChooseView.snp.remakeConstraints { (make) ->Void in
            make.top.equalTo(chooseButton.snp.bottom).offset(10)
            make.left.right.equalTo(chooseButton)
            make.height.equalTo(200)
        }
    }
        
    lazy var bgImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "Reuse_headBg")
        return imageView
    }()
    
    
    lazy var promptLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(12)
        label.textColor = UIColor.white
        label.text = "*仅统计自营订单，不包含平台订单"
        return label
    }()
    
    lazy var promptOneLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(14)
        label.textColor = UIColor.white
        label.text = "订单总金额"
        return label
    }()

    lazy var moneyLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(40)
        label.textColor = UIColor.white
        return label
    }()

    
    lazy var leftView: MCMallStatisticsHeader_itemView = {
        let view = MCMallStatisticsHeader_itemView()
        view.topLabel.text = "订单实付总计"
        view.bottomLabel.text = "0"
        return view
    }()
    
    lazy var centerView: MCMallStatisticsHeader_itemView = {
        let view = MCMallStatisticsHeader_itemView()
        view.topLabel.text = "订单总数"
        view.bottomLabel.text = "¥0"
        return view
    }()
    
    lazy var rightView: MCMallStatisticsHeader_itemView = {
        let view = MCMallStatisticsHeader_itemView()
        view.topLabel.text = "佣金总计(含在途)"
        view.bottomLabel.text = "¥0"
        return view
    }()

    
    lazy var promptTwoLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(20)
        label.textColor = UIColor.MCGray_middle
        label.text = "~"
        return label
    }()

    
    lazy var beginTimeButton: MCReverseHorizontalButton = {
        let button = MCReverseHorizontalButton()
        button.backgroundColor = UIColor.white
        button.iconImageView.image = #imageLiteral(resourceName: "Bill_calendar")
        button.nameLabel.text = "开始时间"
        return button
    }()
    
    lazy var endTimeButton: MCReverseHorizontalButton = {
        let button = MCReverseHorizontalButton()
        button.backgroundColor = UIColor.white
        button.iconImageView.image = #imageLiteral(resourceName: "Bill_calendar")
        button.nameLabel.text = "结束时间"
        return button
    }()
    
    lazy var chooseButton: MCReverseHorizontalButton = {
        let button = MCReverseHorizontalButton()
        button.backgroundColor = UIColor.white
        button.iconImageView.image = #imageLiteral(resourceName: "Ticket_choose")
        button.nameLabel.text = "请选择"
        button.addTarget(self, action: #selector(chooseButtonClicked), for: .touchUpInside)

        return button
    }()
    
    lazy var sureButotn: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 5
        button.backgroundColor = UIColor.MCMain
        button.titleLabel?.font = MCFont(14)
        button.setTitle("确定", for: .normal)
        button.setTitleColor(UIColor.white, for: .normal)

        return button
    }()
    
    lazy var popUpChooseView: MCMallStatisticsChooseObjectView = {
        let view = MCMallStatisticsChooseObjectView()
        return view
    }()
    
    lazy var emptyView: MCEmptyDataView = {
        let view = MCEmptyDataView()
        return view
    }()
}


class MCMallStatisticsHeader_itemView : UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(topLabel)
        topLabel.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(self)
            make.bottom.equalTo(self.snp.centerY).offset(-5)
        }
        
        self.addSubview(bottomLabel)
        bottomLabel.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(self)
            make.top.equalTo(self.snp.centerY).offset(6)
        }

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    lazy var topLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(13)
        label.text = "XXX"
        label.textColor = UIColor.white
        label.textAlignment = NSTextAlignment.center
        return label
    }()

    lazy var bottomLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(13)
        label.text = "0"
        label.textColor = UIColor.white
        label.textAlignment = NSTextAlignment.center
        return label
    }()

}
